//
//  Fraction.m
//  02-ClassAndObjectHomeWork
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
//指定初始化
- (instancetype)initWithNumerator:(NSInteger)numerator Denominator:(NSInteger)denominator
{
    
    self = [super init];
    if (self) {
        _denominator = denominator;
        _numerator = numerator;
    }
    return self;
}
//便利构造器
+ (instancetype)fractionWithNumerator:(NSInteger)numerator Denominator:(NSInteger)denominator
{
    
    return [[Fraction alloc]initWithNumerator:numerator Denominator:denominator];
    
}
#pragma mark 得到约分后的分数
- (CGFloat)getFraction
{
    NSInteger min = _numerator < _denominator ? _numerator : _denominator;
    for (NSInteger i = min; i >= 1; i--) {
        if (_numerator % i == 0 && _denominator % i == 0) {
            min = i;
            break;
        }
    }
    _numerator = _numerator / min;
    _denominator = _denominator / min;
    NSLog(@"约分后的分数: %ld/%ld", _numerator, _denominator);
    return _numerator / _denominator;
}
//最大公约数
- (NSInteger)getMax:(NSInteger)num1 and:(NSInteger)num2
{
    NSInteger min = num1 < num2 ? num1 : num2;
    for (NSInteger i = min; i >= 1; i--) {
        if (num1 % i == 0 && num2 % i == 0) {
            min = i;
            break;
        }
    }
    return min;
    
}
//最小公倍数
- (NSInteger)getMin:(NSInteger)num1 and:(NSInteger)num2
{
    NSInteger max = num1 > num2 ? num1 : num2;
    for (NSInteger i = max ; i <= num1 * num2; i++) {
        if (i % num1 == 0 && i % num2 == 0) {
            max = i;
            break;
        }
    }
    return max;
    
}
//加法
- (void)add:(Fraction *)p and:(Fraction *)q
{
    NSInteger a = [self getMin:p.denominator and:q.denominator];
    NSInteger b = a / p.denominator * p.numerator + a / q.denominator * q.numerator;
    NSLog(@"%ld/%ld + %ld/%ld = %ld/%ld", p.numerator, p.denominator, q.numerator, q.denominator , b, a);
    NSInteger min = a < b ? a : b;
    for (NSInteger i = min; i >= 1; i--) {
        if (a % i == 0 && b % i == 0) {
            min = i;
            break;
        }
    }
    b = b / min;
    a = a / min;
    NSLog(@"约分后的分数: %ld/%ld", b, a);
    
}
//减法
- (void)cut:(Fraction *)p and:(Fraction *)q
{
    NSInteger a = [self getMin:p.denominator and:q.denominator];
    NSInteger b = a / p.denominator * p.numerator - a / q.denominator * q.numerator;
    NSLog(@"%ld/%ld - %ld/%ld = %ld/%ld", p.numerator, p.denominator, q.numerator, q.denominator , b, a);
    NSInteger aa = abs((int)a);
    NSInteger bb = abs((int)b);
    NSInteger min = aa < bb ? aa : bb;
    for (NSInteger i = min; i >= 1; i--) {
        if (aa % i == 0 && bb % i == 0) {
            min = i;
            break;
        }
    }
    b = b / min;
    a = a / min;
    NSLog(@"约分后的分数: %ld/%ld", b, a);
  
}
//乘法
- (void)product:(Fraction *)p and:(Fraction *)q
{
    NSInteger a = p.denominator * q.denominator;
    NSInteger b = p.numerator * q.numerator;
    NSInteger min = a < b ? a : b;
    for (NSInteger i = min; i >= 1; i--) {
        if (a % i == 0 && b % i == 0) {
            min = i;
            break;
        }
    }
    b = b / min;
    a = a / min;
    NSLog(@"%ld/%ld * %ld/%ld = %ld/%ld", p.numerator, p.denominator, q.numerator, q.denominator , b, a);
}
//除法
- (void)divide:(Fraction *)p and:(Fraction *)q
{
    NSInteger a = p.denominator * q.numerator;
    NSInteger b = p.numerator * q.denominator;
    NSInteger min = a < b ? a : b;
    for (NSInteger i = min; i >= 1; i--) {
        if (a % i == 0 && b % i == 0) {
            min = i;
            break;
        }
    }
    b = b / min;
    a = a / min;
    NSLog(@"(%ld/%ld) / (%ld/%ld) = %ld/%ld", p.numerator, p.denominator, q.numerator, q.denominator , b, a);
}
//比较大小
- (void)compare:(Fraction *)p and:(Fraction *)q
{
    NSInteger a = [self getMin:p.denominator and:q.denominator];
    NSInteger b = a / p.denominator * p.numerator - a / q.denominator * q.numerator;
    if (b > 0) {
        NSLog(@"%ld/%ld > %ld/%ld ", p.numerator, p.denominator, q.numerator, q.denominator);
    }
    else if (b < 0)
    {
        NSLog(@"%ld/%ld < %ld/%ld ", p.numerator, p.denominator, q.numerator, q.denominator);
    }
    else
    {
        NSLog(@"%ld/%ld = %ld/%ld ", p.numerator, p.denominator, q.numerator, q.denominator);
    }
}


@end
